#include "./project.h"
// 初始化图片信息
bool album_info(LCD_P lcd)
{
    ALB_P album_strcut = create_album_info();
    if (album_strcut == (ALB_P)-1)
    {
        printf("创建总结构体失败\n");
        return false;
    }
    lcd->album_General = album_strcut;
    bmp_path_search(lcd);
    return true;
}

// 相册总接口
bool main_album(LCD_P lcd)
{
    // 初始化并创建头节点
    album_info(lcd);
    show_white_screen(lcd);
    // 默认显示第一页缩缩略图
    show_bmp_General(lcd, 1);
    Show_One_Thumbnail(lcd, "/ui/exit.bmp", 1, 0, 0);
    int page = 1;
    while (1)
    {
        // 获取方向
        touch_direction(lcd);
        if (lcd->album_General->direction == 1)
        {
            page++;
        }
        else if (lcd->album_General->direction == 3)
        {
            page--;
        }
        if (page > lcd->album_General->bmp_page)
        {
            page = lcd->album_General->bmp_page;
            continue;
        }
        if (page < 1)
        {
            page = 1;
            continue;
        }
        if (lcd->album_General->direction == 2 || lcd->album_General->direction == 4)
        {
            continue;
        }
        if (lcd->album_General->direction == 0)
        {
            if (lcd->touch_x > 0 && lcd->touch_x < 50 && lcd->touch_y > 0 && lcd->touch_y < 50)
            {
                // printf("退出\n");
                // while (lcd->album_General->bmp_header->next != lcd->album_General->bmp_header)
                // {
                //     BMP_P node = lcd->album_General->bmp_header->next;
                //     node->prev->next = node->next;
                //     node->next->prev = node->prev;
                //     node->next = node;
                //     node->prev = node;
                //     free(node);
                // }
                // free(lcd->album_General->bmp_header);
                // free(lcd->album_General);
                // lcd->album_General->bmp_num = 0;
                // lcd->album_General->bmp_page = 1;
                // lcd->album_General = NULL;
                return true;
            }
            // 跳到显示原图的接口
            all_bmp_show_org(lcd, page);
            // printf("原图到这li\n");
            continue;
        }
        // // 显示图接口
        show_white_screen(lcd);
        show_bmp_General(lcd, page);
        Show_One_Thumbnail(lcd, "/ui/exit.bmp", 1, 0, 0);
    }

    return true;
}
// 显所有图片原图
bool all_bmp_show_org(LCD_P lcd, int page)
{
    // printf("%d   %d\n", lcd->touch_x, lcd->touch_y);
    // 获取每个位置是哪一张图
    if (lcd->touch_x > 65 && lcd->touch_x < 265 && lcd->touch_y > 5 && lcd->touch_y < 125)
    {
        lcd->album_General->small_bmp_flag = 0;
    }
    else if (lcd->touch_x > 295 && lcd->touch_x < 495 && lcd->touch_y > 5 && lcd->touch_y < 125)
    {
        lcd->album_General->small_bmp_flag = 1;
    }
    else if (lcd->touch_x > 525 && lcd->touch_x < 725 && lcd->touch_y > 5 && lcd->touch_y < 125)
    {
        lcd->album_General->small_bmp_flag = 2;
    }
    else if (lcd->touch_x > 65 && lcd->touch_x < 265 && lcd->touch_y > 165 && lcd->touch_y < 285)
    {
        lcd->album_General->small_bmp_flag = 3;
    }
    else if (lcd->touch_x > 295 && lcd->touch_x < 495 && lcd->touch_y > 165 && lcd->touch_y < 285)
    {
        lcd->album_General->small_bmp_flag = 4;
    }
    else if (lcd->touch_x > 525 && lcd->touch_x < 725 && lcd->touch_y > 165 && lcd->touch_y < 285)
    {
        lcd->album_General->small_bmp_flag = 5;
    }
    else if (lcd->touch_x > 65 && lcd->touch_x < 265 && lcd->touch_y > 325 && lcd->touch_y < 445)
    {
        lcd->album_General->small_bmp_flag = 6;
    }
    else if (lcd->touch_x > 295 && lcd->touch_x < 495 && lcd->touch_y > 325 && lcd->touch_y < 445)
    {
        lcd->album_General->small_bmp_flag = 7;
    }
    else if (lcd->touch_x > 525 && lcd->touch_x < 725 && lcd->touch_y > 325 && lcd->touch_y < 445)
    {
        lcd->album_General->small_bmp_flag = 8;
    }
    else
    {
        printf("没有被点击11111111111111111\n");
        return true;
    }
    // 寻找所点击的图片是哪一张
    BMP_P bmp_next = lcd->album_General->bmp_header->next;
    int bmp_num = 0;
    for (int i = 0; i < (page - 1) * 9; i++)
    {
        bmp_next = bmp_next->next;
        bmp_num++;
    }
    for (int k = 0; k < lcd->album_General->small_bmp_flag; k++)
    {
        if (bmp_next == lcd->album_General->bmp_header)
        {
            continue;
        }
        bmp_num++;
        bmp_next = bmp_next->next;
    }
    lcd->touch_x = -1;
    lcd->touch_y = -1;
    // 删除并显示原图
    remove_org_bmp(bmp_num, lcd, bmp_next);
    show_white_screen(lcd);
    // BMP_P node = lcd->album_General->bmp_header->next;
    // while (node != lcd->album_General->bmp_header)
    // {

    //     printf("%s------回到未点击页面\n", node->bmp_name);
    //     node = node->next;
    //     sleep(1);
    // }
    // for (int i = 0; i < 9; i++)
    // {
    //     printf("%s------回到未点击页面\n", node->bmp_name);
    //     node = node->next;
    // }

    show_bmp_General(lcd, 1);
    Show_One_Thumbnail(lcd, "/ui/exit.bmp", 1, 0, 0);
    return true;
}
// 删除图片接口
bool remove_org_bmp(int num, LCD_P lcd, BMP_P bmp_next)
{
    int bmp_num = num;
    char num_buf[128] = "\0";
    char page_buf[128] = "\0";
    // 图片路径
    sprintf(num_buf, "%d", (num + 1));
    sprintf(page_buf, "%s/%d", num_buf, lcd->album_General->bmp_num);
    Original_bmp_show(lcd, bmp_next->bmp_path);
    Show_One_Thumbnail(lcd, "/ui/exit.bmp", 1, 0, 0);
    Show_One_Thumbnail(lcd, "/ui/del.bmp", 1, 770, 0);
    line(lcd, page_buf, 360, 0);
    BMP_P new_bmp_node = bmp_next;
    // 原图上下页切换
    while (1)
    {
        touch_direction(lcd);
        if (lcd->album_General->direction == 1)
        {
            if (new_bmp_node->next == lcd->album_General->bmp_header)
            {
                bmp_num = lcd->album_General->bmp_num - 1;

                Show_One_Thumbnail(lcd, new_bmp_node->bmp_path, 1, 0, 0);
            }
            else
            {
                bmp_num++;
                Show_One_Thumbnail(lcd, new_bmp_node->next->bmp_path, 1, 0, 0);
                new_bmp_node = new_bmp_node->next;
            }
        }
        if (lcd->album_General->direction == 3)
        {
            if (new_bmp_node->prev == lcd->album_General->bmp_header)
            {
                bmp_num = 0;
                Show_One_Thumbnail(lcd, new_bmp_node->bmp_path, 1, 0, 0);
            }
            else
            {
                bmp_num--;
                Show_One_Thumbnail(lcd, new_bmp_node->prev->bmp_path, 1, 0, 0);
                new_bmp_node = new_bmp_node->prev;
            }
        }
        if (lcd->album_General->direction == 0)
        {
            if (lcd->touch_x > 0 && lcd->touch_x < 60 && lcd->touch_y > 0 && lcd->touch_y < 60)
            {
                lcd->touch_x = -1;
                lcd->touch_y = -1;
                return true;
            }
            if (lcd->touch_x > 720 && lcd->touch_x < 800 && lcd->touch_y > 0 && lcd->touch_y < 60)
            {

                bmp_next->prev->next = bmp_next->next;
                bmp_next->next->prev = bmp_next->prev;
                bmp_next->next = bmp_next;
                bmp_next->prev = bmp_next;
                free(bmp_next);
                lcd->album_General->bmp_num--;
                if (lcd->album_General->bmp_num % 9 == 0)
                {
                    lcd->album_General->bmp_page--;
                }
                // printf("%d\n", lcd->album_Gener al->bmp_num);
                // printf("%d\n", lcd->album_General->bmp_page);
                // BMP_P node = lcd->album_General->bmp_header->next;
                // while (node != lcd->album_General->bmp_header)
                // {
                //     printf("%s------回到未点击页面\n", node->bmp_name);
                //     node = node->next;
                //     sleep(1);
                // }

                return true;
            }
            continue;
        }
        memset(num_buf, 0, sizeof(num_buf));
        memset(page_buf, 0, sizeof(page_buf));
        sprintf(num_buf, "%d", (bmp_num + 1));
        sprintf(page_buf, "%s/%d", num_buf, lcd->album_General->bmp_num);
        line(lcd, page_buf, 360, 0);
        Show_One_Thumbnail(lcd, "/ui/exit.bmp", 1, 0, 0);
        Show_One_Thumbnail(lcd, "/ui/del.bmp", 1, 770, 0);
    }
    return true;
}
// 创建总结构体
ALB_P create_album_info()
{
    ALB_P album_strcut = (ALB_P)malloc(sizeof(ALB));
    if (album_strcut == (ALB_P)NULL)
    {
        perror("mallalum");
        return (ALB_P)-1;
    }
    memset(album_strcut, 0, sizeof(ALB));
    BMP_P bmp_header = create_bmp_node();
    if (bmp_header == (BMP_P)-1)
    {
        perror("创建头节点失败\n");
        return (ALB_P)-1;
    }
    album_strcut->bmp_page = 1;
    album_strcut->bmp_header = bmp_header;
    album_strcut->small_bmp_flag = -1;
    return album_strcut;
}
// 创建BMP节点
BMP_P create_bmp_node()
{
    BMP_P bmp_struct = (BMP_P)malloc(sizeof(BMP));
    if (bmp_struct == (BMP_P)NULL)
    {
        perror("bmp_malloc");
        return (BMP_P)-1;
    }
    memset(bmp_struct, 0, sizeof(BMP));
    bmp_struct->next = bmp_struct;
    bmp_struct->prev = bmp_struct;
    return bmp_struct;
}
// 添加bmp节点
bool add_bmp_node(LCD_P lcd, BMP_P node)
{
    if (lcd->album_General == (ALB_P)NULL || node == (BMP_P)NULL)
    {
        perror("空节点");
        return false;
    }
    lcd->album_General->bmp_header->prev->next = node;
    node->prev = lcd->album_General->bmp_header->prev;
    node->next = lcd->album_General->bmp_header;
    lcd->album_General->bmp_header->prev = node;

    ++(lcd->album_General->bmp_num);
    if ((lcd->album_General->bmp_num) % 10 == 0)
    {
        lcd->album_General->bmp_page++;
    }
    return true;
}
// 显示图片总接口
bool show_bmp_General(LCD_P lcd, int page)
{
    int where_x = 65;
    int where_y = 5;
    BMP_P node_next = lcd->album_General->bmp_header->next;
    for (int i = 0; i < (page - 1) * 9; i++)
    {
        node_next = node_next->next;
    }
    for (int i = 0; i < 9; i++)
    {
        if (node_next == lcd->album_General->bmp_header)
        {
            break;
        }
        show_small_bmp(lcd, node_next, 4, where_x, where_y);
        line(lcd, node_next->bmp_name, where_x + 40, where_y + 125);
        node_next = node_next->next;
        if (where_x >= 470)
        {
            where_y += 160;
            where_x = 65;
        }
        else
        {
            where_x += 230;
        }
    }
    return true;
}
// 显示缩列图
bool show_small_bmp(LCD_P lcd, BMP_P node, int zoom, int where_x, int where_y)
{
    int fp_bmp = open(node->bmp_path, O_RDONLY);
    if (fp_bmp == -1)
    {
        perror("5");
        return false;
    }
    int bmp_x, bmp_y;
    lseek(fp_bmp, 18, SEEK_SET);
    if (read(fp_bmp, &bmp_x, 4) == -1)
    {
        perror("6");
        return false;
    }
    if (read(fp_bmp, &bmp_y, 4) == -1)
    {
        perror("7");
        return false;
    }
    lseek(fp_bmp, 54, SEEK_SET);
    int skip = 4 - (bmp_x * 3 % 4);
    if (skip == 4)
    {
        skip = 0;
    }
    char rgb[bmp_x * bmp_y * 3 + bmp_y * skip];
    memset(rgb, 0, sizeof(rgb));
    if (read(fp_bmp, rgb, sizeof(rgb)) == -1)
    {
        perror("8");
        return false;
    }
    int *new_map = lcd->map_dev + 800 * where_y + where_x;
    int n = 0;
    int yu = bmp_x % zoom;
    for (int y = 0; y < bmp_y / zoom; y++)
    {
        for (int x = 0; x < bmp_x / zoom; x++, n += (3 * zoom))
        {
            *(new_map + bmp_x * (bmp_y / zoom - 1 - y) + x) = rgb[n] << 0 | rgb[n + 1] << 8 | rgb[n + 2] << 16;
        }
        n += skip;
        n += yu * 3;
        n += bmp_x * 3 * (zoom - 1) + skip * (zoom - 1);
    }
    if (close(fp_bmp) == -1)
    {
        perror("eeeeeee");
        return false;
    }
    return true;
}
// 显示原图
bool Original_bmp_show(LCD_P lcd, char *bmp_path)
{
    int fp_bmp = open(bmp_path, O_RDONLY);
    if (fp_bmp == -1)
    {
        perror("51111111111");
        return false;
    }
    int bmp_x, bmp_y;

    lseek(fp_bmp, 18, SEEK_SET);
    if (read(fp_bmp, &bmp_x, 4) == -1)
    {
        perror("6");
        return false;
    }
    if (read(fp_bmp, &bmp_y, 4) == -1)
    {
        perror("7");
        return false;
    }

    lseek(fp_bmp, 54, SEEK_SET);
    int skip = 4 - (bmp_x * 3 % 4);
    if (skip == 4)
    {
        skip = 0;
    }
    char rgb[bmp_x * bmp_y * 3 + bmp_y * skip];
    memset(rgb, 0, sizeof(rgb));
    if (read(fp_bmp, rgb, sizeof(rgb)) == -1)
    {
        perror("8");
        return false;
    }
    int *new_map = lcd->map_dev;
    int n = 0;
    for (int y = 0; y < bmp_y; y++)
    {
        for (int x = 0; x < bmp_x; x++, n += 3)
        {
            *(new_map + bmp_x * (bmp_y - 1 - y) + x) = rgb[n] << 0 | rgb[n + 1] << 8 | rgb[n + 2] << 16;
        }
    }
    if (close(fp_bmp) == -1)
    {
        perror("");
        return false;
    }
    return true;
}

bool Show_One_Thumbnail(LCD_P lcd, const char *bmp_path, int zoomout, int bmp_x, int bmp_y)
{
    int *tmp_mmap_p = lcd->map_dev + lcd->lcd_w * bmp_y + bmp_x;
    // 打开图片
    int bmp = open(bmp_path, O_RDONLY);
    if (bmp == -1)
    {
        perror("bmp ...");
        return false;
    }

    // 获取图片w和h
    int bmp_w, bmp_h;
    lseek(bmp, 18, SEEK_SET);
    if (read(bmp, &bmp_w, sizeof(int)) == -1)
    {
        perror("Read w ...");
        return false;
    }

    if (read(bmp, &bmp_h, sizeof(int)) == -1)
    {
        perror("Read h ...");
        return false;
    }

    // 计算skip
    int skip = 0;
    if (bmp_w * 3 % 4 != 0)
    {
        skip = 4 - (bmp_w * 3 % 4);
    }

    // 定义数组
    int bmp_size = bmp_w * bmp_h * 3 + bmp_h * skip;
    // printf("即将缩小%d倍显示宽：%d-高：%d的%s图片--- %d\n",zoomout,bmp_w,bmp_h,bmp_path,skip);

    char rgb[bmp_size];

    // read读取图片像素点 + 补齐字节数
    lseek(bmp, 54, SEEK_SET);

    if (read(bmp, rgb, bmp_size) == -1)
    {
        perror("read ...");
        return false;
    }

    for (int y = 0, n = 0; y < bmp_h / zoomout; y++)
    {

        for (int x = 0; x < bmp_w / zoomout; x++, n += (3 * zoomout))
        {

            *(tmp_mmap_p + 800 * (bmp_h / zoomout - 1 - y) + x) = rgb[n] << 0 | rgb[n + 1] << 8 | rgb[n + 2] << 16;
        }

        // 跳补数
        n += skip;

        // 需要跳剩余的像素点字节数
        n += (bmp_w % zoomout) * 3;

        // 跳行数 跳 zoomout-1行数 字节数
        n += bmp_w * (zoomout - 1) * 3 + skip * (zoomout - 1);
    }
    if (close(bmp) == -1)
    {
        perror("close ...");
        return false;
    }

    return true;
}
// 销毁bmp节点
bool del_bmp_node(LCD_P lcd)
{
    while (lcd->album_General->bmp_header->next != lcd->album_General->bmp_header)
    {
        BMP_P node_next = lcd->album_General->bmp_header->next;
        lcd->album_General->bmp_header->next = node_next->next;
        node_next->next->prev = lcd->album_General->bmp_header;
        node_next->next = node_next;
        node_next->prev = node_next;
        free(node_next);
    }
    return true;
}